package com.syp.auth.web.controller;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.syp.auth.common.Constants;
import com.syp.auth.domain.enums.ResourceTypeEnum;
import com.syp.auth.domain.vo.ResourceVo;
import com.syp.auth.domain.vo.easyui.Json;
import com.syp.auth.domain.vo.easyui.TreeNode;
import com.syp.auth.export.annotation.SecurityNone;
import com.syp.auth.export.common.GeneralUserVo;
import com.syp.auth.export.common.GorgeousParameters;
import com.syp.auth.export.common.TokenCooikeMaster;
import com.syp.auth.export.resource.AuthorizeResourceI;
import com.syp.auth.export.util.CurrentUserHolder;
import com.syp.auth.service.base.ResourceServiceI;
import com.syp.auth.service.base.UserServiceI;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/sys/resource")
public class ResourceController {

	@Autowired
	private AuthorizeResourceI authorizeResource;
	@Autowired
	private ResourceServiceI resourceService = null;
	@Autowired
	private UserServiceI userService = null;

	/**
	 * 资源主页
	 * 
	 * @return
	 */
	@RequestMapping("/main")
	public ModelAndView mainPage(@CookieValue(GorgeousParameters.AUTH_COOKIE_NAME) String sid) {
		ModelAndView mv = new ModelAndView("sys/resource/resource");
		GeneralUserVo guser = CurrentUserHolder.get();
		if (guser == null) {
			guser = this.authorizeResource.cuser(sid);
		}
		List<String> permitUrlList = guser.getPermitUrlList();
		mv.addObject("permitUrlList", permitUrlList);
		return mv;
	}

	/**
	 * 资源新增页面
	 * 
	 * @return
	 */
	@RequestMapping("/addPage")
	public String addPage() {
		return "sys/resource/resourceAddEdit";
	}

	/**
	 * 资源编辑页面
	 * 
	 * @return
	 */
	@RequestMapping("/editPage")
	public String editPage() {
		return "sys/resource/resourceAddEdit";
	}

	/**
	 * 资源查看页面
	 * 
	 * @return
	 */
	@RequestMapping("/showPage")
	public String showPage() {
		return "sys/resource/resourceShow";
	}

	/**
	 * 资源添加
	 * 
	 * @param resource
	 * @return 返回结果json
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Json add(ResourceVo resource) {
		Json json = new Json();
		try {
			this.resourceService.add(resource);
			json.setSuccess(true);
			json.setMsg("添加成功！");
		} catch (Exception e) {
			log.error("添加失败。", e);
			json.setMsg("添加失败！");
		}

		return json;
	}

	/**
	 * 根据ID获取资源对象
	 * 
	 * @param id
	 * @return 返回结果json
	 */
	@RequestMapping("/getObject")
	@ResponseBody
	public ResourceVo getObject(Long id) {
		ResourceVo resource = null;
		try {
			resource = this.resourceService.get(id);
		} catch (Exception e) {
			log.error("根据ID查询异常。", e);
		}

		return resource;
	}

	/**
	 * 编辑资源
	 * 
	 * @param resource
	 * @return 返回结果json
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(ResourceVo resource) {
		Json json = new Json();
		try {
			this.resourceService.edit(resource);
			json.setSuccess(true);
			json.setMsg("编辑成功！");
		} catch (Exception e) {
			log.error("编辑资源异常。", e);
			json.setMsg("编辑失败！");
		}

		return json;
	}

	/**
	 * 删除资源
	 * 
	 * @param id
	 * @return 返回结果json
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(Long id) {
		Json json = new Json();
		try {
			this.resourceService.delete(id);
			json.setSuccess(true);
			json.setMsg("删除成功！");
			json.setObj(id);
		} catch (Exception e) {
			log.error("删除资源异常。", e);
			json.setMsg("删除失败！");
		}

		return json;
	}

	/**
	 * 资源树状列表
	 * 
	 * @param root
	 * @return 返回结果json
	 */	
	@RequestMapping("/allResGrid")
	@ResponseBody
	public Object allResGrid(ResourceVo root, Boolean recursive) {
		Object object = null;
		try {
			object = this.resourceService.treegrid(root, null, null, recursive).getChildren();
			if (object == null) {
				object = new Object();
			}
		} catch (Exception e) {
			log.error("获取资源数异常", e);
			object = new Object();
		}

		return object;
	}
	
	@RequestMapping("/allResTree")
	@ResponseBody
	public Object allResTree(TreeNode root, Boolean recursive) {
		Object object = new Object();
		try {
			object = this.resourceService.tree(root, null, null, recursive).getChildren();
		} catch (Exception e) {
			log.error("获取资源树异常", e);
		}

		return object;
	}
	
	@RequestMapping("/myResTree")
	@ResponseBody
	public Object myResTree(TreeNode root, Boolean recursive, HttpServletRequest request) {
		Object object = new Object();
		try {
			Cookie token = TokenCooikeMaster.getInstunce().get(request);
			if (token != null) {
				GeneralUserVo guser = CurrentUserHolder.get();
				if (guser == null) {
					guser = this.authorizeResource.cuser(token.getValue());
				}
				List<Long> myResIdList = null;
				if (guser.getId() != Constants.SUPER_MAN_NO) {
					myResIdList = this.userService.myResIdList(guser.getId());
					for (ResourceVo r : guser.getPermitResList()) {
						myResIdList.add(r.getId());
					}
				}
				object = this.resourceService.tree(root, myResIdList, null, recursive).getChildren();
				
			}
		} catch (Exception e) {
			log.error("获取资源树异常", e);			
		}

		return object;
	}
	
	@RequestMapping("/menu")
	@ResponseBody
	public Object menu(TreeNode root, HttpServletRequest request) {
		Object object = new Object();
		try {
			Cookie token = TokenCooikeMaster.getInstunce().get(request);
			if (token != null) {
				GeneralUserVo guser = CurrentUserHolder.get();
				if (guser == null) {
					guser = this.authorizeResource.cuser(token.getValue());
				}
				List<Long> permitMenuList = new LinkedList<>();
				for (ResourceVo r : guser.getPermitResList()) {
					if (r.getResType() != null && r.getResType() == ResourceTypeEnum.MENU.getType()) {
						permitMenuList.add(r.getId());
					}					
				}
				object = this.resourceService.menuTree(root, permitMenuList, false).getChildren();
			}
		} catch (Exception e) {
			log.error("获取菜单树异常", e);
		}

		return object;
	}

	@SecurityNone
	@RequestMapping("/checkResCodeUnique")
	@ResponseBody
	public Json checkResCodeUnique(String resCode) {
		Json json = new Json();
		try {
			boolean unique = this.resourceService.checkResNameUnique(resCode);
			json.setSuccess(unique);
			json.setMsg(unique ? "资源代码唯一!" : "资源代码已存在!");
		} catch (Exception e) {
			log.error("根据资源边吗查询资源异常。", e);
		}

		return json;

	}
}
